﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using DigitsRecognizer.UI;

namespace TheVoiceOfSnake.BussinesLogic
{
    public class MovesTrigger
    {
        public GameForm GameForm;
        public Thread Thread;
        private delegate void SetCallback();

        public MovesTrigger(GameForm gameForm) 
        {
            GameForm = gameForm;
        }

        public void Start()
        {
            ThreadStart wstart = new ThreadStart(Actualize);
            Thread = new Thread(wstart);
            Thread.Start();
        }

        public void Stop()
        {
            Thread.Abort();
        }

        public void Actualize()
        {
            while (true)
            {
                Thread.Sleep(1000);
                if (GameForm.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.
                    SetCallback t = new SetCallback(GameForm.RefreshBoard);
                    GameForm.Invoke
                        (t, new object[] { });
                }
                else
                {
                    // It's on the same thread, no need for Invoke 
                    GameForm.RefreshBoard();
                }
            }
        }
    }
}
